package logic

import (
	"context"
	"shopping/model/mysql"

	"shopping/shoppingrpc/internal/svc"
	"shopping/shoppingrpc/shoppingrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *shoppingrpc.GoodsListRequest) (*shoppingrpc.GoodsListResponse, error) {
	// todo: add your logic here and delete this line

	var goods []mysql.Goods

	mysql.DB.Find(&goods)

	var user []*shoppingrpc.GoodsList
	for _, good := range goods {
		list := shoppingrpc.GoodsList{
			Id:         int64(good.ID),
			Name:       good.Name,
			Price:      float32(good.Price),
			Image:      good.Image,
			Stock:      int64(good.Stock),
			Status:     int64(good.Status),
			CategoryId: int64(good.CategoryId),
		}
		user = append(user, &list)
	}

	return &shoppingrpc.GoodsListResponse{
		List: user,
	}, nil
}
